package btd.game.dlgs;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

import btd.game.ConstData;
import btd.game.OrderAdapter;
import btd.game.activities.R;
import btd.game.activities.SdeskAct;
import btd.game.activities.SroomAct;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class OrderDlg extends Dialog {
	private Context owner = null;
	// private Button bBack = null;
	private Button bStartGame = null;
	private ListView orderList = null;
	// private String[] from = null;
	// private int[] to = null;
	// private SimpleAdapter mAdapter = null;
	private OrderAdapter orderAdapter = null;

	// private ArrayList<HashMap<String, String>> items = null;
	public OrderDlg(Context context) {
		super(context);
		owner = context;
		this.setTitle(R.string.ordertext);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.order);

		// bBack = (Button) findViewById(R.id.ButtonOrderDlgBack);
		// bBack.setOnClickListener(backListener);
		bStartGame = (Button) findViewById(R.id.ButtonOrderStartGame);
		bStartGame.setOnClickListener(startGAmeListener);
		orderList = (ListView) findViewById(R.id.ListViewOrderList);
		orderList.setFocusable(false);
		// from = new String[] { "id", "name", "r", "gj", "gq" };
		// to = new int[] { R.id.TextViewOrderInfoID,
		// R.id.TextViewOrderInfoName,
		// R.id.TextViewOrderInfoR, R.id.TextViewOrderInfoGuijia,
		// R.id.ImageViewOrderInfoGQ };
		// items = new ArrayList<HashMap<String, String>>();
		// for (int i = 0; i < 100; i++) {
		// addItem(String.valueOf(i + 1), "WWW", "R0000", "CHN", "guoqi_bs");
		// }
		// mAdapter = new SimpleAdapter(owner, items, R.layout.orderinfo, from,
		// to);
		orderAdapter = new OrderAdapter(owner);
		String[] cl = ConstData.getCountryJCList();
		Random rd = new Random();
		int[] rs = new int[100];
		int nowIndex = 1;
		for (int i = 0; i < 100; i++) {
			rs[i] = rd.nextInt(2500);
			// orderAdapter.addItem(String.valueOf(i + 1), "WWW", "R0000",
			// cl[rd.nextInt(234)]);
		}
		ConstData.insertSort(rs, "d");
		for (int i : rs) {
			orderAdapter.addItem(String.valueOf(nowIndex++), String
					.valueOf((char) ('A' + rd.nextInt(26))), "R" + i, cl[rd
					.nextInt(234)]);
		}
		orderList.setAdapter(orderAdapter);
	}

	/*
	 * private View.OnClickListener backListener = new View.OnClickListener() {
	 * public void onClick(View v) { ((SroomAct) owner).back();
	 * OrderDlg.this.cancel(); } };
	 */

	private View.OnClickListener startGAmeListener = new View.OnClickListener() {
		public void onClick(View v) {
			OrderDlg.this.cancel();
		}
	};

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		super.onKeyDown(keyCode, event);
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// ((SroomAct) owner).back();
			OrderDlg.this.cancel();
		}
		return false;
	}

	/*
	 * private void addItem(String deskid, String name, String r, String gj,
	 * String gq) { HashMap<String, String> i = new HashMap<String, String>();
	 * i.put("id", deskid + ". "); i.put("name", name + " "); i.put("r", r +
	 * " "); i.put("gj", "(" + gj + ")"); i.put("gq", gq); items.add(i); }
	 */
}
